package cn.youqu.happy.module.youqu.controller.admin.userJoinActivity;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userJoinActivity.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userJoinActivity.UserJoinActivityDO;
import cn.youqu.happy.module.youqu.convert.userJoinActivity.UserJoinActivityConvert;
import cn.youqu.happy.module.youqu.service.userJoinActivity.UserJoinActivityService;

@Api(tags = "管理后台 - 用户加入活动")
@RestController
@RequestMapping("/youqu/user-join-activity")
@Validated
public class UserJoinActivityController {

    @Resource
    private UserJoinActivityService userJoinActivityService;

    @PostMapping("/create")
    @ApiOperation("创建用户加入活动")
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:create')")
    public CommonResult<Integer> createUserJoinActivity(@Valid @RequestBody UserJoinActivityCreateReqVO createReqVO) {
        return success(userJoinActivityService.createUserJoinActivity(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户加入活动")
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:update')")
    public CommonResult<Boolean> updateUserJoinActivity(@Valid @RequestBody UserJoinActivityUpdateReqVO updateReqVO) {
        userJoinActivityService.updateUserJoinActivity(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户加入活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:delete')")
    public CommonResult<Boolean> deleteUserJoinActivity(@RequestParam("id") Integer id) {
        userJoinActivityService.deleteUserJoinActivity(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户加入活动")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:query')")
    public CommonResult<UserJoinActivityRespVO> getUserJoinActivity(@RequestParam("id") Integer id) {
        UserJoinActivityDO userJoinActivity = userJoinActivityService.getUserJoinActivity(id);
        return success(UserJoinActivityConvert.INSTANCE.convert(userJoinActivity));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户加入活动列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:query')")
    public CommonResult<List<UserJoinActivityRespVO>> getUserJoinActivityList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserJoinActivityDO> list = userJoinActivityService.getUserJoinActivityList(ids);
        return success(UserJoinActivityConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户加入活动分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:query')")
    public CommonResult<PageResult<UserJoinActivityRespVO>> getUserJoinActivityPage(@Valid UserJoinActivityPageReqVO pageVO) {
        PageResult<UserJoinActivityDO> pageResult = userJoinActivityService.getUserJoinActivityPage(pageVO);
        return success(UserJoinActivityConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户加入活动 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-join-activity:export')")
    @OperateLog(type = EXPORT)
    public void exportUserJoinActivityExcel(@Valid UserJoinActivityExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserJoinActivityDO> list = userJoinActivityService.getUserJoinActivityList(exportReqVO);
        // 导出 Excel
        List<UserJoinActivityExcelVO> datas = UserJoinActivityConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户加入活动.xls", "数据", UserJoinActivityExcelVO.class, datas);
    }

}
